package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.common.Result;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Logging;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.ILoggingService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 自定义日志记录表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-17
 */
@RestController
@RequestMapping("/logging")
public class LoggingController {
    
@Resource
private ILoggingService loggingService;

        // 新增或者更新
        @MyLog(value = "新增或更新系统日志")
        @PostMapping
        public Result save(@RequestBody Logging logging) {
                return Result.success(loggingService.saveOrUpdate(logging));
                }

        @RequiresPermissions("系统日志")
        @MyLog(value = "单个删除系统日志")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(loggingService.removeById(id));
                }

        @RequiresPermissions("系统日志")
        @MyLog(value = "批量删除系统日志")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(loggingService.removeByIds(ids));
                }

        @RequiresPermissions("系统日志")
        @MyLog(value = "查询所有系统日志")
        @GetMapping
        public Result findAll() {
                return Result.success(loggingService.list());
                }

        @RequiresPermissions("系统日志")
        @MyLog(value = "查询单个系统日志")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(loggingService.getById(id));
                }

        @RequiresPermissions("系统日志")
        @MyLog(value = "分页查询系统日志")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String levelString) {
                QueryWrapper<Logging> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("created_time");
                if (!"".equals(levelString)) {
                        queryWrapper.like("level_string", levelString);
                }
                return Result.success(loggingService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }

        }

